﻿using AdinZ.Web.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/4/7 18:08:04
* */
namespace Web
{
    public partial class search : BasePage
    {
        Novel.Sys.Model.novel_article model_art = new Novel.Sys.Model.novel_article();
        Novel.Sys.BLL.novel_article bll_art = new Novel.Sys.BLL.novel_article();
        protected void Page_Load(object sender, EventArgs e)
        {
            string vKeyWord;
            vKeyWord = Request.QueryString["key"];
            if (vKeyWord == null) { vKeyWord = ""; }
            vKeyWord = AdinZ.Common.DataValidate.SqlTextClear(vKeyWord);

            string rPage;
            rPage = Request.QueryString["page"];

            int vPage = 1;
            if (rPage != string.Empty && rPage != null && AdinZ.Common.DataValidate.IsNumber(rPage))
            {
                vPage = Convert.ToInt32(rPage);
            }
            string vHtml = Fun_GetSearchContent(vPage, vKeyWord);
            vHtml = Function.Fun_UrlRewriter(vHtml);

            Response.Write(vHtml);

        }

        private string Fun_GetSearchContent(int byPage, string byKeyWord)
        {
            string vTemplateUrl = "search.html";
            Template wt = new Template();
            wt.Load_Template(vTemplateUrl);
            wt.vPage = byPage;

            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:title}"), siteConfig.WebName, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:filehost}"), siteConfig.FileHost, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:sitehost}"), siteConfig.SiteHost, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:seotitle}"), seoConfig.SeoTitle, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:keywords}"), byKeyWord, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:description}"), seoConfig.IndexDescription, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:sitepath}"), "<a href=\"" + siteConfig.SiteHost + "\">首页</a>&nbsp;>&nbsp;网站搜索", RegexOptions.IgnoreCase);

            //搜索专用-开始
            string vSqlWhere = "[Display]=1";
            int vCountNum = 0;
            string vErrorInfo = "";

            if (byKeyWord.Length < 2 || byKeyWord.Length > 10)
            {
                vErrorInfo = "关键字不能大于10,小于2";
                wt.vContent = Fun_GetShowContent(wt.vContent, "yesinfo");
                wt.vContent = Fun_GetShowContent(wt.vContent, "noinfo");
            }
            else
            {
                vSqlWhere += " And [title] Like '%" + byKeyWord + "%' OR author LIKE '%" + byKeyWord + "%' OR intor LIKE '%" + byKeyWord + "%'";

                DataSet ds = bll_art.GetList(vSqlWhere);
                vCountNum = ds.Tables[0].Rows.Count;

                //过滤掉相应的内容
                if (vCountNum == 0)
                {
                    wt.vContent = Fun_GetShowContent(wt.vContent, "yesinfo");
                    wt.vContent = Fun_GetShowContent(wt.vContent, "errorinfo");
                }
                else
                {
                    wt.vContent = Fun_GetShowContent(wt.vContent, "noinfo");
                    wt.vContent = Fun_GetShowContent(wt.vContent, "errorinfo");
                }
            }

            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{search:keyword}"), byKeyWord, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{search:countnum}"), vCountNum.ToString(), RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{search:error}"), vErrorInfo, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{search:sqlwhere}"), vSqlWhere, RegexOptions.IgnoreCase);
            //搜索专用-结束

            //wt.Parser_MyTag();
            wt.Parser_List();
            wt.Parser_Page();
            wt.Parser_IF();
            return wt.vContent;
        }

        private string Fun_GetShowContent(string byContent, string byTagsstr)
        {
            Regex regex = new Regex(@"<!--(search:" + byTagsstr + @")-->([\s\S]*?)<!--search:" + byTagsstr + "-->", RegexOptions.IgnoreCase);
            MatchCollection matchCollection = regex.Matches(byContent);
            foreach (Match m in matchCollection)
            {
                byContent = byContent.Replace(m.Value, "");
            }
            return byContent;
        }



    }
}